package com.dubbo.provider.serviceimpl;

import com.dubbo.api.PaperService;
import com.dubbo.api.model.BrowserModel;
import com.dubbo.api.model.PaperModel;
import com.dubbo.api.model.UserModel;
import com.dubbo.provider.entity.Browser;
import com.dubbo.provider.entity.Paper;
import com.dubbo.provider.mapper.BrowserMapper;
import com.dubbo.provider.mapper.PaperMapper;
import org.apache.dubbo.config.annotation.Service;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class PaperServiceImpl implements PaperService {

    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private BrowserMapper browserMapper;

    @Override
    public List<PaperModel> listPaper() {
        List<Paper> paperList = paperMapper.listPaper();

        List<PaperModel> userModelList = paperList.stream().map(paper -> {
            return this.convertPaperModelFromPaper(paper);
        }).collect(Collectors.toList());
        return userModelList;
    }

    @Override
    public PaperModel count() {
        int count = paperMapper.countPaper();
        if (count == 0){
            return null;
        }
        PaperModel paperModel = new PaperModel();
        paperModel.setCount(count);
        return paperModel;
    }

    //将Paper-》paperModel
    private PaperModel convertPaperModelFromPaper(Paper paper) {
        if (paper == null) {
            return null;
        }
        PaperModel paperModel = new PaperModel();
        BeanUtils.copyProperties(paper, paperModel);
        //单独设置时间，mysql是sql.date,model是joda-date
        //paperModel.setpDate(new DateTime(paper.getpDate()));
        return paperModel;
    }


}
